//权限
define(function() {


	var ViewModel = function() {
		var self = this;
		/** zTree树* */
		/** ztree */
		var ztree = "";
		var pztree = "";
		/** 绑定数据* */
		var rId = "";
		// 角色树
		self.ztreeshow = function() {
			var tree = {
				pFunction : {
					zTree : '',
					setting : {
						view : {
							dblClickExpand : false,
							selectedMulti : false,
							showTitle : false,
							showIcon : true
						},
						check : {
							enable : false
						},
						data : {
							key : {
								name : "roleName"
							},
							simpleData : {
								enable : true,
								idKey : "id",
								pIdKey : "pId",
								rootPId : null
							}
						},
						callback : {
							onClick : self.zTreeOnClick
						}
					}
				}
			};
			$.ajax({
				url : "/role/findRoleZtree",
				type : "post",
				dataType : "json",
				contentType : 'application/json;charset=UTF-8',
				success : function(result) {
					var zNodes = result;
					ztree = $.fn.zTree.init($("#treeDemo"),
							tree.pFunction.setting, zNodes);
					ztree.expandAll(true);
				}
			})
		};
		// 刷新
		self.l_sx = function() {
			self.ztreeshow();
		}
		// 左侧角色树全部展开
		self.l_zk = function() {
			ztree.expandAll(true);
		}
		// 左侧角色树全部收缩
		self.l_ss = function() {
			ztree.expandAll(false);
		}
		// 角色树点击事件
		self.zTreeOnClick = function(event, treeId, treeNode) {
			// 语句隐藏
			$(".icon_back").css("display", "none");
			// 获取角色Id
			var node = ztree.getSelectedNodes();
			rId = node[0].id;
			// 角色Id获取已有的权限
			var dataArr=[];
			$.ajax({
				url : "/roleAuthority/findPermissionsByRId",
				data : JSON.stringify(rId),
				type : "post",
				dataType : "json",
				contentType : "application/json;charset=UTF-8",
				success : function(data) {
					// 获取已有的权限数组
					console.log(data);
					 dataArr = data;
					 self.ztreeshows(dataArr);
				}
			});
			
		}
		// 权限树
		self.ztreeshows = function(dataArr) {
			var tree = {
				pFunction : {
					zTree : '',
					setting : {
						view : {
							dblClickExpand : false,
							selectedMulti : false,
							showTitle : false,
							showIcon : true
						},
						check : {
							enable : true,
							chkStyle : "checkbox"
						},
						data : {
							simpleData : {
								enable : true,
								idKey : "id",
								pIdKey : "pid",
								rootPId : null
							}
						},
						callback : {
							onClick : self.pzTreeOnClick
						}
					}
				}
			};
			$.ajax({
				url : "/roleAuthority/findZtreePermissions",
				type : "post",
				dataType : "json",
				contentType : 'application/json;charset=UTF-8',
				success : function(result) {
					var zNodes = result;
					pztree = $.fn.zTree.init($("#ptreeDemo"),
							tree.pFunction.setting, zNodes);
					for (var i = 0; i < dataArr.length; i++) {
						pztree.checkNode(pztree.getNodeByParam("id",
								dataArr[i], null), true, true);
					}
					pztree.expandAll(true);
				}
			})
		};
		// 权限树点击事件
		self.pzTreeOnClick = function(event, treeId, treeNode) {
			var node = pztree.getSelectedNodes();
			for (var i = 0, l = node.length; i < l; i++) {
				pztree.checkNode(node[i], true, true);
			}

		};
		// 权限树保存事件
		self.save = function() {
			var nodes = pztree.getCheckedNodes(true);
			var arr = [];
			for (var i = 0; i < nodes.length; i++) {
				// 获取角色的权限Id
				arr.push(nodes[i].id);
			}
			if(arr==null){
				alert("为选择权限");
			}
			layer.confirm('确实要修改权限吗?', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				 layer.msg('确定', {icon: 1});
				// 获取角色的权限数组
				// 向后台传送权限数组
				var data = {};
				data.id = rId;
				data.list = arr;
				$.ajax({
					url : "/roleAuthority/updatePermissions",
					type : "post",
					dataType : "json",
					traditional : true,
					data : JSON.stringify(data),
					contentType : 'application/json;charset=UTF-8',
					success : function(result) {
						if (result.status == 0) {
							toastr.success(result.data, result.msg);
						} else {
							toastr.error(result.msg);
						}
					}
				})
				})
		};

		self.l_sx();
		// 右侧权限树全部展开
		self.r_zk = function() {
			pztree.expandAll(true);
		}
		// 右侧权限树全部收缩
		self.r_ss = function() {
			pztree.expandAll(false);
		}
	}
	var model = new ViewModel();
	ko.applyBindings(model);
});